package gitee.cheungming.modules.dmp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import gitee.cheungming.common.service.impl.CrudServiceImpl;
import gitee.cheungming.common.utils.ConvertUtils;
import gitee.cheungming.modules.dmp.dao.DeviceDao;
import gitee.cheungming.modules.dmp.dto.DeviceDTO;
import gitee.cheungming.modules.dmp.entity.DeviceEntity;
import gitee.cheungming.modules.dmp.service.DeviceService;
import cn.hutool.core.util.StrUtil;
import gitee.cheungming.modules.security.user.SecurityUser;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 设备
 *
 * @author cheungming cheungming@gmail.com
 * @since 1.0.0 2025-08-21
 */
@Service
public class DeviceServiceImpl extends CrudServiceImpl<DeviceDao, DeviceEntity, DeviceDTO> implements DeviceService {

    @Override
    public QueryWrapper<DeviceEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");
        String deviceNumber = (String) params.get("deviceNumber");

        QueryWrapper<DeviceEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);
        wrapper.like(StrUtil.isNotBlank(deviceNumber), "device_number", deviceNumber);

        return wrapper;
    }


    @Override
    public void save(DeviceDTO dto) {
        DeviceEntity deviceEntity = ConvertUtils.sourceToTarget(dto, DeviceEntity.class);
        deviceEntity.setCreator(SecurityUser.getUserId());
        deviceEntity.setCreateDate(new Date());
        insert(deviceEntity);
    }

    @Override
    public void update(DeviceDTO dto) {
        DeviceEntity deviceEntity = ConvertUtils.sourceToTarget(dto, DeviceEntity.class);
        deviceEntity.setUpdater(SecurityUser.getUserId());
        deviceEntity.setUpdateDate(new Date());
        updateById(deviceEntity);
    }
}